package com.bawei.module_inspection.fl

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_inspection.R
import com.bawei.module_inspection.databinding.FragmentInspectionStandardBinding
import com.bawei.module_inspection.fl.ui.InspectionStandardViewModel
import com.bawei.module_inspection.ui.InspectionViewModel
import kotlinx.coroutines.launch


class InspectionStandardFragment : Fragment() {


    private lateinit var binding: FragmentInspectionStandardBinding
    private val viewModel: InspectionStandardViewModel by viewModels()
    private val standardAdapter by lazy { StandardListAdapter() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentInspectionStandardBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 初始化 RecyclerView
        binding.rvStandard.apply {
            adapter = standardAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }

        // 监听数据变化
        lifecycleScope.launch {
            viewModel.standardList.collect { data ->
                if (data.isNotEmpty()) {
                    standardAdapter.submitList(data)
                    binding.rvStandard.visibility = View.VISIBLE
                    binding.ivEmpty.visibility = View.GONE
                    // 若有空白布局，可隐藏：binding.ivEmpty.visibility = View.GONE
                } else {
                    // 无数据时显示空白布局（如图片+提示文字）
                    binding.rvStandard.visibility = View.GONE
                    binding.ivEmpty.visibility = View.VISIBLE
                }
            }
        }
    }


}
// 列表适配器
class StandardListAdapter : ListAdapter<String, StandardViewHolder>(DiffUtilCallback()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StandardViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_standard, parent, false)
        return StandardViewHolder(view)
    }

    override fun onBindViewHolder(holder: StandardViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
}

class StandardViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    fun bind(item: String) {
        itemView.apply {
            itemView.findViewById<TextView>(R.id.tv_standard_text).text = item
        }
    }
}

class DiffUtilCallback : DiffUtil.ItemCallback<String>() {
    override fun areItemsTheSame(oldItem: String, newItem: String) = oldItem == newItem
    override fun areContentsTheSame(oldItem: String, newItem: String) = oldItem == newItem
}